﻿//-------------------------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2011 , Hairihan TECH, Ltd.
//-------------------------------------------------------------------------------------

using System;
using System.Data;

namespace DotNet.Business
{
    using DotNet.BaseManager;
    using DotNet.DbUtilities;
    using DotNet.Model;
    using DotNet.Utilities;

    /// <summary>
    /// BaseParameterManager
    /// 参数类
    ///
    /// 修改纪录
    ///
    ///     2009.04.01 版本：2.1 JiRiGaLa   创建者、修改者进行完善。
    ///     2008.04.30 版本：2.0 JiRiGaLa   按面向对象，面向服务进行改进。
    ///     2007.06.08 版本：1.4 JiRiGaLa   重新调整方法。
    ///		2006.02.05 版本：1.3 JiRiGaLa	重新调整主键的规范化。
    ///		2006.01.28 版本：1.2 JiRiGaLa	对一些方法进行改进，主键整理，调用性能也进行了修改，主键顺序进行整理。
    ///		2005.08.13 版本：1.1 JiRiGaLa	主键整理好。
    ///		2004.11.12 版本：1.0 JiRiGaLa	主键进行了绝对的优化，这是个好东西啊，平时要多用，用得要灵活些。
    ///
    /// 版本：2.0
    ///
    /// <author>
    ///		<name>JiRiGaLa</name>
    ///		<date>2008.04.30</date>
    /// </author>
    /// </summary>
    public class BaseParameterManager : BaseManager
    {
        public BaseParameterManager()
        {
            base.CurrentTableName = BaseParameterTable.TableName;
        }

        public BaseParameterManager(IDbHelper dbHelper)
            : this()
        {
            DbHelper = dbHelper;
        }

        public BaseParameterManager(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            UserInfo = userInfo;
        }

        #region protected string AddEntity(BaseParameterEntity parameterEntity)

        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="parameterEntity">实体对象</param>
        /// <returns>主键</returns>
        protected string AddEntity(BaseParameterEntity parameterEntity)
        {
            BaseSequenceManager sequenceManager = new BaseSequenceManager(DbHelper);
            string sequence = sequenceManager.GetSequence(BaseParameterTable.TableName);
            SQLBuilder sqlBuilder = new SQLBuilder(DbHelper);
            sqlBuilder.BeginInsert(BaseParameterTable.TableName);
            sqlBuilder.SetValue(BaseParameterTable.FieldId, sequence);
            sqlBuilder.SetValue(BaseParameterTable.FieldCategoryId, parameterEntity.CategoryId);
            sqlBuilder.SetValue(BaseParameterTable.FieldParameterId, parameterEntity.ParameterId);
            sqlBuilder.SetValue(BaseParameterTable.FieldParameterCode, parameterEntity.ParameterCode);
            sqlBuilder.SetValue(BaseParameterTable.FieldWorked, parameterEntity.Worked ? 1 : 0);
            sqlBuilder.SetValue(BaseParameterTable.FieldParameterContent, parameterEntity.ParameterContent);
            sqlBuilder.SetValue(BaseParameterTable.FieldEnabled, parameterEntity.Enabled ? 1 : 0);
            sqlBuilder.SetValue(BaseParameterTable.FieldDescription, parameterEntity.Description);
            sqlBuilder.SetValue(BaseParameterTable.FieldCreateUserId, UserInfo.Id);
            sqlBuilder.SetValue(BaseParameterTable.FieldCreateBy, UserInfo.RealName);
            sqlBuilder.SetValue(BaseParameterTable.FieldModifiedBy, UserInfo.RealName);
            sqlBuilder.SetDBNow(BaseParameterTable.FieldCreateOn);
            return sqlBuilder.EndInsert() > 0 ? sequence : string.Empty;
        }

        #endregion protected string AddEntity(BaseParameterEntity parameterEntity)

        #region protected int UpdateEntity(BaseParameterEntity parameterEntity) 更新

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="parameterEntity">实体</param>
        /// <returns>影响行数</returns>
        protected int UpdateEntity(BaseParameterEntity parameterEntity)
        {
            SQLBuilder sqlBuilder = new SQLBuilder(DbHelper);
            sqlBuilder.BeginUpdate(BaseParameterTable.TableName);
            sqlBuilder.SetValue(BaseParameterTable.FieldCategoryId, parameterEntity.CategoryId);
            sqlBuilder.SetValue(BaseParameterTable.FieldParameterCode, parameterEntity.ParameterCode);
            sqlBuilder.SetValue(BaseParameterTable.FieldParameterId, parameterEntity.ParameterId);
            sqlBuilder.SetValue(BaseParameterTable.FieldParameterContent, parameterEntity.ParameterContent);
            sqlBuilder.SetValue(BaseParameterTable.FieldWorked, parameterEntity.Worked ? 1 : 0);
            sqlBuilder.SetValue(BaseParameterTable.FieldEnabled, parameterEntity.Enabled ? 1 : 0);
            sqlBuilder.SetValue(BaseParameterTable.FieldModifiedUserId, UserInfo.Id);
            sqlBuilder.SetValue(BaseParameterTable.FieldModifiedBy, UserInfo.RealName);
            sqlBuilder.SetDBNow(BaseParameterTable.FieldModifiedOn);
            sqlBuilder.SetWhere(BaseParameterTable.FieldId, parameterEntity.Id);
            return sqlBuilder.EndUpdate();
        }

        #endregion protected int UpdateEntity(BaseParameterEntity parameterEntity) 更新

        #region public string Add(string categoryId, string parameterId, string parameterCode, string parameterContent, bool worked, bool enabled)

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="categoryId">类别主键</param>
        /// <param name="parameterId">标记主键</param>
        /// <param name="parameterCode">标记编码</param>
        /// <param name="parameterContent">标记内容</param>
        /// <param name="worked">工作状态</param>
        /// <param name="enabled">有效</param>
        /// <returns>主键</returns>
        public string Add(string categoryId, string parameterId, string parameterCode, string parameterContent, bool worked, bool enabled)
        {
            BaseParameterEntity parameterEntity = new BaseParameterEntity();
            parameterEntity.CategoryId = categoryId;
            parameterEntity.ParameterId = parameterId;
            parameterEntity.ParameterCode = parameterCode;
            parameterEntity.ParameterContent = parameterContent;
            parameterEntity.Worked = worked;
            parameterEntity.Enabled = enabled;
            return this.Add(parameterEntity);
        }

        #endregion public string Add(string categoryId, string parameterId, string parameterCode, string parameterContent, bool worked, bool enabled)

        #region public string Add(BaseParameterEntity parameterEntity)

        /// <summary>
        /// 添加内容
        /// </summary>
        /// <param name="parameterEntity">内容</param>
        /// <returns>主键</returns>
        public string Add(BaseParameterEntity parameterEntity)
        {
            // 此处检查this.exist()
            string returnValue = string.Empty;
            string[] names = new string[3];
            Object[] values = new Object[3];
            names[0] = BaseParameterTable.FieldCategoryId;
            names[1] = BaseParameterTable.FieldParameterId;
            names[2] = BaseParameterTable.FieldParameterCode;
            values[0] = parameterEntity.CategoryId;
            values[1] = parameterEntity.ParameterId;
            values[2] = parameterEntity.ParameterCode;
            if (this.Exists(names, values))
            {
                // 编号已重复
                this.ReturnStatusCode = StatusCode.ErrorCodeExist.ToString();
            }
            else
            {
                returnValue = this.AddEntity(parameterEntity);
                // 运行成功
                this.ReturnStatusCode = StatusCode.OKAdd.ToString();
            }
            return returnValue;
        }

        #endregion public string Add(BaseParameterEntity parameterEntity)

        #region public int Update(BaseParameterEntity parameterEntity) 更新

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="parameterEntity">参数基类表结构定义</param>
        /// <returns>影响行数</returns>
        public int Update(BaseParameterEntity parameterEntity)
        {
            int returnValue = 0;

            // 检查是否已被其他人修改
            //if (DbLogic.IsModifed(DbHelper, BaseParameterTable.TableName, parameterEntity.Id, parameterEntity.ModifiedUserId, parameterEntity.ModifiedOn))
            //{
            //    // 数据已经被修改
            //    this.ReturnStatusCode = StatusCode.ErrorChanged.ToString();
            //}
            //else
            //{
            // 检查编号是否重复
            if (this.Exists(BaseParameterTable.FieldParameterCode, parameterEntity.ParameterCode, parameterEntity.Id))
            {
                // 文件夹名已重复
                this.ReturnStatusCode = StatusCode.ErrorCodeExist.ToString();
            }
            else
            {
                // 进行更新操作
                returnValue = this.UpdateEntity(parameterEntity);
                if (returnValue == 1)
                {
                    this.ReturnStatusCode = StatusCode.OKUpdate.ToString();
                }
                else
                {
                    // 数据可能被删除
                    this.ReturnStatusCode = StatusCode.ErrorDeleted.ToString();
                }
            }

            // }
            return returnValue;
        }

        #endregion public int Update(BaseParameterEntity parameterEntity) 更新

        #region public string GetParameter(string categoryId, string parameterId, string parameterCode)

        /// <summary>
        /// 获取参数
        /// </summary>
        /// <param name="categoryId">类别主键</param>
        /// <param name="flagID">标志主键</param>
        /// <param name="paramFlagCode">编码</param>
        /// <returns>数据集</returns>
        public string GetParameter(string categoryId, string parameterId, string parameterCode)
        {
            string returnValue = string.Empty;
            string[] names = new string[3];
            names[0] = BaseParameterTable.FieldCategoryId;
            names[1] = BaseParameterTable.FieldParameterId;
            names[2] = BaseParameterTable.FieldParameterCode;
            Object[] values = new Object[3];
            values[0] = categoryId;
            values[1] = parameterId;
            values[2] = parameterCode;
            return this.GetProperty(names, values, BaseParameterTable.FieldParameterContent).ToString();
        }

        #endregion public string GetParameter(string categoryId, string parameterId, string parameterCode)

        #region public int SetParameter(string categoryId, string parameterId, string parameterCode, string parameterContent)

        /// <summary>
        /// 更新参数设置
        /// </summary>
        /// <param name="categoryId">类别主键</param>
        /// <param name="parameterId">标志主键</param>
        /// <param name="parameterCode">编码</param>
        /// <param name="parameterContent">参数内容</param>
        /// <returns>影响行数</returns>
        public int SetParameter(string categoryId, string parameterId, string parameterCode, string parameterContent)
        {
            int returnValue = 0;
            string[] names = new string[3];
            names[0] = BaseParameterTable.FieldCategoryId;
            names[1] = BaseParameterTable.FieldParameterId;
            names[2] = BaseParameterTable.FieldParameterCode;
            Object[] values = new Object[3];
            values[0] = categoryId;
            values[1] = parameterId;
            values[2] = parameterCode;

            // 检测是否无效数据
            if ((parameterContent == null) || (parameterContent.Length == 0))
            {
                returnValue = this.BatchDelete(names, values);
            }
            else
            {
                // 检测是否存在
                returnValue = this.SetProperty(names, values, BaseParameterTable.FieldParameterContent, parameterContent);
                if (returnValue == 0)
                {
                    // 进行增加操作
                    BaseParameterEntity parameterEntity = new BaseParameterEntity();
                    parameterEntity.CategoryId = categoryId;
                    parameterEntity.ParameterId = parameterId;
                    parameterEntity.ParameterCode = parameterCode;
                    parameterEntity.ParameterContent = parameterContent;
                    this.AddEntity(parameterEntity);
                    returnValue = 1;
                }
            }
            return returnValue;
        }

        #endregion public int SetParameter(string categoryId, string parameterId, string parameterCode, string parameterContent)

        #region public DataTable GetDTByParameter(string categoryId, string parameterId)

        /// <summary>
        /// 获取记录
        /// </summary>
        /// <param name="categoryId">类别主键</param>
        /// <param name="flagID">标志主键</param>
        /// <param name="paramFlagCode">编码</param>
        /// <returns>数据表</returns>
        public DataTable GetDTByParameter(string categoryId, string parameterId)
        {
            string[] names = new string[2];
            names[0] = BaseParameterTable.FieldCategoryId;
            names[1] = BaseParameterTable.FieldParameterId;
            Object[] values = new Object[2];
            values[0] = categoryId;
            values[1] = parameterId;
            return this.GetDT(names, values);
        }

        #endregion public DataTable GetDTByParameter(string categoryId, string parameterId)

        #region public DataTable GetDTParameterCode(string categoryId, string parameterId, string parameterCode)

        /// <summary>
        /// 获取记录
        /// </summary>
        /// <param name="categoryId">类别主键</param>
        /// <param name="parameterId">标志主键</param>
        /// <param name="parameterCode">编码</param>
        /// <returns>数据表</returns>
        public DataTable GetDTParameterCode(string categoryId, string parameterId, string parameterCode)
        {
            string[] names = new string[3];
            names[0] = BaseParameterTable.FieldCategoryId;
            names[1] = BaseParameterTable.FieldParameterId;
            names[2] = BaseParameterTable.FieldParameterCode;
            Object[] values = new Object[3];
            values[0] = categoryId;
            values[1] = parameterId;
            values[2] = parameterCode;
            return this.GetDT(names, values);
        }

        #endregion public DataTable GetDTParameterCode(string categoryId, string parameterId, string parameterCode)

        #region public int DeleteByParameter(string categoryId, string parameterId)

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="categoryId">类别主键</param>
        /// <param name="flagID">标志主键</param>
        /// <returns>影响行数</returns>
        public int DeleteByParameter(string categoryId, string parameterId)
        {
            string[] names = new string[2];
            names[0] = BaseParameterTable.FieldCategoryId;
            names[1] = BaseParameterTable.FieldParameterId;
            Object[] values = new Object[2];
            values[0] = categoryId;
            values[1] = parameterId;
            return this.BatchDelete(names, values);
        }

        #endregion public int DeleteByParameter(string categoryId, string parameterId)

        #region public int DeleteByParameterCode(string categoryId, string parameterId, string parameterCode)

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="categoryId">类别主键</param>
        /// <param name="parameterId">标志主键</param>
        /// <param name="parameterCode">标志编号</param>
        /// <returns>影响行数</returns>
        public int DeleteByParameterCode(string categoryId, string parameterId, string parameterCode)
        {
            string[] names = new string[3];
            names[0] = BaseParameterTable.FieldCategoryId;
            names[1] = BaseParameterTable.FieldParameterId;
            names[2] = BaseParameterTable.FieldParameterCode;
            Object[] values = new Object[3];
            values[0] = categoryId;
            values[1] = parameterId;
            values[2] = parameterCode;
            return this.BatchDelete(names, values);
        }

        #endregion public int DeleteByParameterCode(string categoryId, string parameterId, string parameterCode)
    }
}